﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SnmpForm.ApSearch
{
    public partial class FrmSearchTarget : Form
    {
        public FrmSearchTarget()
        {
            InitializeComponent();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            byte []fromip =  ipAddressControlFrom.GetAddressBytes();
            System.Net.IPAddress addrFrom = new System.Net.IPAddress(fromip);
            byte[] toip = ipAddressControlTo.GetAddressBytes();
            System.Net.IPAddress addrTo = new System.Net.IPAddress(toip);

            //改判定有错
            if (IPAddressExtend.LessOrEquals(addrFrom, addrTo))
            {
                DbWrapper.DbDataTypes.SearchTarget target =
                    new DbWrapper.DbDataTypes.SearchTarget(
                        addrFrom.ToString(),
                        addrTo.ToString());
                bool bSuccess = BusinessWrapper.User.Instance.AddSearchTarget(target);
                if (bSuccess)
                {
                    MessageBox.Show("添加成功");
                    UpdateSearchTargetView();
                }
                else
                {
                    MessageBox.Show("添加失败");
                }
            }
            else
            {
                MessageBox.Show("开始位置必须小于结束位置");
            }
        }

        private void FrmSearchTarget_Load(object sender, EventArgs e)
        {
            //listView
            listViewTarget.View = View.Details;
            listViewTarget.FullRowSelect = true;
            listViewTarget.Columns.Add("编号");
            listViewTarget.Columns.Add("起始IP", 200);
            listViewTarget.Columns.Add("结束IP", 200);

            UpdateSearchTargetView();
        }

        private void UpdateSearchTargetView()
        {
            listViewTarget.Items.Clear();

            List<DbWrapper.DbDataTypes.SearchTarget> listTarget =
                BusinessWrapper.User.Instance.GetSearchTargetList();
            foreach (DbWrapper.DbDataTypes.SearchTarget target in listTarget)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Text = target.id.ToString();
                lvi.SubItems.Add(target.from);
                lvi.SubItems.Add(target.to);
                listViewTarget.Items.Add(lvi);
            }
        }

        private DbWrapper.DbDataTypes.SearchTarget GetSelectedTarget()
        {
            DbWrapper.DbDataTypes.SearchTarget target = null;
                
            if (listViewTarget.SelectedItems.Count > 0)
            {
                target = new DbWrapper.DbDataTypes.SearchTarget();
                ListViewItem item = listViewTarget.SelectedItems[0];
                target.id = Convert.ToInt32(item.Text);
            }
            return target;
        }

        private void ToolStripMenuItemDel_Click(object sender, EventArgs e)
        {
            DbWrapper.DbDataTypes.SearchTarget target =
                GetSelectedTarget();
            bool bSuccess =
                BusinessWrapper.User.Instance.DelSearchTarget(target);
            if (bSuccess)
            {
                MessageBox.Show("删除成功");
                UpdateSearchTargetView();
            }
            else
            {
                MessageBox.Show("删除失败");
            }
        }

        private void contextMenuStripSearchTarget_Opening(object sender, CancelEventArgs e)
        {
            if (GetSelectedTarget() == null)
            {
                e.Cancel = true;
            }
        }
    }
}
